<?php
defined('BASEPATH') or die('forbidden');

class pullrolelevel_controller extends Controller {

    private $inc;
    
    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));

        $this->inc=$this->config['inc'];
    }

    function index() {
        //print_r($_SESSION['gkl']);
        $areahostArray=array();
        $server_list=get_server_list();
        foreach ($server_list as $k=>$v){
            $areahostArray[$v['host']] = $v['id']."-".$v['name'];
        }
        $selectedArea = $_SESSION['area_selected_id'];
        $this->view->assign("arealist", $this->form->select("area_selected_id", $areahostArray, $selectedArea, array('id' => 'area_selected_id')));
        $this->view->display("seal/pullrolelevel/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $sql="select * from pullrolelevel where 1=1 ";
        if (empty($area_host))  
        {
        	$sql.=" and area_host='".$area_host."'";
        }
        $sql.=" order by insert_time desc LIMIT {$offset},{$rp}";
        $this->db->query($sql);
        $row=$this->db->row('default');
        
        
        foreach ($row as $key=>$value) {
            $value['name']  =$value['role_name'];
            $rows[] = array('id' => $value['id'], 'cell' => $value);
        }
        $sql = "SELECT COUNT(*) AS num FROM pullrolelevel where 1=1 ";
        if (empty($area_host))  
        {
        	$sql.=" and area_host='".$area_host."'";
        }
        $one = $this->db->one($sql);
        $total = $one['num'];
        
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        echo $_SESSION['EYO'];
        if (is_submit()) {
            $rowdata = $_POST['row'];
            var_dump($rowdata);

            $post_data['SessionID']="ewewqw";
            $post_data['SessionKey']="wqqew";
            $post_data['TargetID']=(int)$rowdata['id'];
            $post_data['Level']=(int)$rowdata['level'];
            $post_data['VipLevel']=(int)$rowdata['vip_level'];
            $post_data['Exp']=(int)$rowdata['exp'];
            foreach ($rowdata['item'] as $k=>$v){
                $item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
                $post_data['Actions'][$k]=$item;
            }
            foreach ($rowdata['money_item'] as $k=>$v){
                $item=array("ID"=>(int)$v,"Num"=>(int)$rowdata['amount'][$k]);
                $post_data['Moneys'][$k]=$item;
            }
            var_dump(json_encode($post_data));

            $area_host=$rowdata['area_host'];
//            unset($rowdata['area_host']);
//            $_SESSION['role_name']=$rowdata['role_name'];
            $_SESSION['area_host']=$area_host;
            //print_r($rowdata);
            if(!empty($rowdata['level']))
            {

                $strResult = vpost($area_host.'/gm_set_player_info',3,json_encode($post_data));
                $_SESSION['EYO']=$strResult;

                $result=json_decode($strResult,true);
                //操作失败或者连接失败
                if($result['RetCode'] == 0)
                {
                    $rowdata['insert_time']=get_datetime(time());
                    $arealist=get_area_list_host();
                    $rowdata['area_name']=$arealist[$area_host];
                    $rowdata['area_host']=$area_host;
                    unset($rowdata['name']);
                   // $this->db->insert('pullrolelevel',$rowdata);
                    insert_log(translate('MENU_SEAL_PULLROLELEVEL'),translate('ADD'),NULL,array("name"=>$rowdata['role_name'].'/'.$rowdata['level']));
                    cp_tips("success");
                }
                else
                {
                    cp_tips("error");
                }
            }
            else
            {
                $url = $area_host . 'role_name=' . $rowdata['role_name'] . '&action=37';                
                $strResult = vpost($url);
                $result=0;
                if ($strResult != 0 || $strResult === FALSE) {
                    $result=1;
                } else {                  
                    //insert_log(translate('MENU_SEAL_PULLROLELEVEL'),translate('MENU_SEAL_PULL_ONLINE'),NULL,array("name"=>$rowdata['role_name']));
                }
                if($strResult===FALSE)
                {
                    $result=  translate('C_CONNECT_AREA_FIALED');
                }
                else
                {
                    $result=$this->inc['operationResult'][$strResult];
                }
                echo "<script>alert('".$result."');</script>";
            }          
        }
        $area_list=array();
        $server_list=get_server_list();
        foreach ($server_list as $k=>$v){
            $area_list[$v['SvrOutAddr']] = $v['SvrID']."-".$v['SvrName'];
        }
        $this->view->assign('role_name', $_SESSION['role_name']);
        $this->view->assign("arealist", $this->form->select("row[area_host]", $area_list, $_SESSION['area_host'], array('id' => 'area_host')));
        $this->view->display("seal/pullrolelevel/add");
    }
}

?>
